package com.windchimes.edueval_platform.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.windchimes.edueval_platform.common.AppConstants;
import com.windchimes.edueval_platform.common.CustomException;
import com.windchimes.edueval_platform.common.R;
import com.windchimes.edueval_platform.domain.po.Teacher;
import com.windchimes.edueval_platform.domain.vo.TeacherVO;
import com.windchimes.edueval_platform.service.ITeacherService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 教师即管理员管理
 */
@Slf4j
@RestController
@RequestMapping("/teachers")
@RequiredArgsConstructor
public class TeacherController {
    private final ITeacherService teacherService;

    /**
     * 新增教师
     *
     * @param teacher 教师对象，包含要新增教师的信息
     * @return 返回新增教师后的提示信息
     */
    @PostMapping
    public R<String> save(@RequestBody Teacher teacher){
        log.info("新增教师信息：{}",teacher.toString());
        teacher.setPassword(DigestUtils.md5DigestAsHex(AppConstants.DEFAULT_PASSWORD.getBytes()));
//        teacher.setCreateTime(LocalDateTime.now());
//        teacher.setUpdateTime(LocalDateTime.now());
//
//        Long id = 1L;
//        teacher.setCreateUser(id);
//        teacher.setUpdateUser(id);

        teacherService.save(teacher);
        return R.success(null,"新增教师成功");
    }

    /**
     * 教师登录
     * @param request 请求对象，用于获取请求信息
     * @param teacher 教师对象，包含登录信息
     * @return 登录结果
     */
    @PostMapping("/login")
    public R<String> login( HttpServletRequest request,@RequestBody Teacher teacher){

        // 1. 调用 Service 进行登录验证
        Teacher loginTeacher = teacherService.login(teacher);

        // 2. 存储 Session（属于 Web 层逻辑）
        request.getSession().setAttribute("teacher", loginTeacher.getId());

        // 3. 返回统一响应
        return R.success("登录成功");
    }

    /**
     * 退出登录
     * @param request 请求对象，用于获取请求信息
     * @return 退出登录结果
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("teacher");
        return R.success(null,"退出成功");
    }

    /**
     * 分页查询教师信息
     * @param page 页码
     * @param pageSize 每页记录数
     * @param name 教师姓名
     * @return
     */
    @GetMapping("/page")
    public R<Page<TeacherVO>> page(int page, int pageSize, String name){
        Page<TeacherVO> pageInfo = teacherService.getTeacherPage(page,pageSize,name);
        return R.success(pageInfo,null);
    }

    /**
     * 根据id查询教师信息
     * @param id 教师id
     * @return
     */
    @GetMapping("/{id}")
    public R<TeacherVO> getById (@PathVariable Long id){
        TeacherVO teacherVO = teacherService.getTeacherVOById(id);
        return R.success(teacherVO, "查询成功");
    }

    /**
     * 批量修改教师状态
     * @param status 传过来0表示要把status修改为0；
     * @param ids 批量修改的id集合
     * @return 返回修改结果
     */
    @PostMapping("/status/{status}")
    public R<String> setStatus(@PathVariable int status, @RequestParam List<Long> ids){

        teacherService.setStatus(status,ids);

        return R.success(null,"修改成功");
    }

    @PutMapping
    public R<String> update(@RequestBody Teacher teacher){

        teacherService.updateById(teacher);
        return R.success(null,"修改成功");
    }
}
